#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

class Singleton{
public:
    static Singleton * getInstance(){
        pthread_once(&_once,init);
        return _pInstance;
    }

    friend class AutoRelease;
private:
    static void init(){
        _pInstance = new Singleton();
    }

    static pthread_once_t _once;
private:
    Singleton(){}
    ~Singleton(){}
    Singleton(const Singleton & rhs) = delete;
    Singleton & operator=(const Singleton & rhs) = delete;
    static Singleton *_pInstance;
};
Singleton * Singleton::_pInstance = nullptr;
pthread_once_t Singleton::_once =PTHREAD_ONCE_INIT;

class AutoRelease{
public:
    AutoRelease(Singleton * s)
        :_s(s)
    {

    }
    ~AutoRelease(){
        if(_s){
            delete _s;
            _s = nullptr;
            Singleton::_pInstance = nullptr;
        }
    }
private:
    Singleton * _s;
};

void test(){
    AutoRelease ar1 (Singleton::getInstance());
    //ar1.~AutoRelease();
    //AutoRelease ar2 (Singleton::getInstance());
}

int main()
{
    test();
    return 0;
}

